package calendar;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

/* this class provides a panel to list the events in a calendar(s) */
class EventListPanel extends CalendarPanel implements ItemListener {
  
  private static final long serialVersionUID = 448938349L;

  CalendarObjInt calendar;
  JLabel title;
  ButtonGroup buttonGroup;
  HashMap eventList;
  HashMap eventLabels;
  Event selectedEvent;
  CreateEventPanel createEventPanel;
  StatusPanel statusPanel;

  public EventListPanel( CreateEventPanel createEventPanel,
                         StatusPanel statusPanel )
  {
    this.createEventPanel = createEventPanel;
    this.statusPanel = statusPanel;
    title = new JLabel( " Event List [Event Description (Event Owner)]:" );
    eventList = new HashMap();
    eventLabels = new HashMap();
    buttonGroup = new ButtonGroup();
    selectedEvent = null;

    setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );

    add( title );
  }

  public void setCalendar( CalendarObjInt calendar )
  {
    this.calendar = calendar;
  }

  public Event getSelectedEvent()
  {
    return selectedEvent;
  }

  /* reset the event list (clear it) */
  public void resetList()
  {
    eventList.clear();
    eventLabels.clear();
    this.removeAll();
    add( title );
    updateUI();
  }

  /* list all of the events of the specified user(s) */
  public void listUserEvents( ArrayList users )
  {
    Iterator itrUsers = users.iterator();
    while ( itrUsers.hasNext() )
    {
      User user = (User)itrUsers.next();
      ArrayList events = new ArrayList();

      try
      {
        if ( statusPanel.ignoreDates )
          events = calendar.retrieveEvents( user, null, null );
        else
          events = calendar.retrieveEvents( user,
            createEventPanel.getStartDate(), createEventPanel.getEndDate() );
      }
      catch (IOException ex)
      {
        System.out.println( "EventListPanel: listUserEvents -> " + ex );
      }

      /* add all this user's events to the event list */
      if ( events != null )
      {
        Iterator itr = events.iterator();
        while ( itr.hasNext() )
        {
          Event e = (Event)itr.next();
          eventList.put( (Object)new Integer((int)e.getId()), (Object)e );
        }
      }
    }

    /* go through all events and determine if it should be displayed/hidden */
    Set set = eventList.entrySet();
    Iterator itr = set.iterator();
    while ( itr.hasNext() )
    {
      try
      {
        Map.Entry entry = (Map.Entry)itr.next();
        Event e = (Event)entry.getValue();
        Integer i = (Integer)entry.getKey();

        /* check for permission to see this event */
        if(     e.getOwner().equals(calendar.getUser()) ||
                e.getType() == 2 ||
                e.getType() == 3 )
        {
          String label = new String();
          if ( e.getType() == 3 && !e.isParticipant( calendar.getUser() ) )
          {
            label = "[hidden]";
          }
          else
          {
            label = e.getDesc() + " ( " + e.getOwner().getName() + " )";
          }
          eventLabels.put( (Object)i, new JRadioButton( label ) );
          ((JRadioButton)eventLabels.get( i )).addItemListener( this);
        }
      }
      catch(IOException ex)
      {
        System.out.println( "EventListPanel: listUserEvents -> " + ex );
      }
    }

    updateList();
  }

  /* add an event to the list with radio button */
  void updateList()
  {  
    Set set = eventLabels.entrySet();
    Iterator itr = set.iterator();
    while ( itr.hasNext() )
    {
      Map.Entry entry = (Map.Entry)itr.next();
      JRadioButton l = (JRadioButton)entry.getValue();
      add( l );
      buttonGroup.add( l );
    }
  
    updateUI();
  }

  /* monitor for selected radio buttons and trigger appropriate actions */
  public void itemStateChanged( ItemEvent e )
  {
    int eventId = -1;

    Object source = e.getItemSelectable();

    Set set = eventLabels.entrySet();
    Iterator itr = set.iterator();
    while ( itr.hasNext() )
    {
      Map.Entry entry = (Map.Entry)itr.next();
      Integer id = (Integer)entry.getKey();

      if ( source == (JRadioButton)eventLabels.get( id ) )
      {
        if ( e.getStateChange() == ItemEvent.SELECTED )
        {
          this.selectedEvent = (Event)eventList.get( id );
        }
      }
    }

    eventId = CalEvent.SHOW_EVENT;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void actionPerformed(ActionEvent e) {

    int eventId = -1;

    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }
}
